@echo off
echo ================================
echo    Nginx 启动脚本
echo ================================

:: 设置nginx目录
set NGINX_DIR=E:\Program Files (x86)\nginx-1.25.3

:: 检查nginx是否已安装
if not exist "%NGINX_DIR%\nginx.exe" (
    echo 错误: 未找到Nginx，请确保已安装到 %NGINX_DIR%
    echo 如需安装nginx，请访问: http://nginx.org/en/download.html
    pause
    exit /b 1
)

:: 检查配置文件是否存在
if not exist "%NGINX_DIR%\conf\nginx.conf" (
    echo 警告: 未找到nginx配置文件，将使用默认配置
)

:: 检查nginx是否已在运行
echo 检查nginx运行状态...
tasklist /FI "IMAGENAME eq nginx.exe" 2>NUL | find /I /N "nginx.exe">NUL
if "%ERRORLEVEL%"=="0" (
    echo nginx已在运行中，正在重启...
    call :stop_nginx
    timeout /t 2 >nul
)

:: 启动nginx
echo 启动nginx...
cd /d "%NGINX_DIR%"
start "" "%NGINX_DIR%\nginx.exe"

:: 等待启动完成
timeout /t 3 >nul

:: 检查启动状态
tasklist /FI "IMAGENAME eq nginx.exe" 2>NUL | find /I /N "nginx.exe">NUL
if "%ERRORLEVEL%"=="0" (
    echo.
    echo ================================
    echo nginx启动成功！
    echo ================================
    echo 管理后台地址: http://localhost/admin
    echo H5前端地址: http://localhost/h5
    echo API接口地址: http://localhost/api
    echo nginx配置文件: %NGINX_DIR%\conf\nginx.conf
    echo nginx错误日志: %NGINX_DIR%\logs\error.log
    echo nginx访问日志: %NGINX_DIR%\logs\access.log
    echo.
    echo 常用命令:
    echo - 重启nginx: nginx.exe -s reload
    echo - 停止nginx: nginx.exe -s stop
    echo - 测试配置: nginx.exe -t
    echo ================================
) else (
    echo 错误: nginx启动失败
    echo 请检查错误日志: %NGINX_DIR%\logs\error.log
    pause
    exit /b 1
)

echo 按任意键继续...
pause
exit /b 0

:stop_nginx
echo 停止nginx进程...
taskkill /F /IM nginx.exe >nul 2>&1
exit /b 0
